﻿//====================================================================
// Author: Jason Plante
// Date Created: 29 October 2007
// LINQ to Lucene: Copyright (c) 2007.  All rights reserved.
//====================================================================

using System;
using System.Reflection;
using Lucene.Linq.Mapping;
using Lucene.Net.Documents;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Lucene.Linq.VSTests.Mapping
{
    /// <summary>
    ///This is a test class for FieldAttribute and is intended
    ///to contain all FieldAttribute Unit Tests
    ///</summary>
    [TestClass]
    public class TestFieldAttribute
    {
        /// <summary>
        /// Tests the Constructor and its assignment of values of the FieldAttribute Attribute class.
        /// </summary>
        [TestMethod]
        public void TestConstructor()
        {
            // Properties
            Type sample = typeof (SampleClass);
            PropertyInfo[] allProperties = sample.GetProperties();

            Assert.AreEqual(3, allProperties.Length);

            // Id Field
            PropertyInfo id = sample.GetProperty("Id");
            object[] idFields = id.GetCustomAttributes(typeof (FieldAttribute), false);
            Assert.AreEqual(1, idFields.Length);
            var idField = idFields[0] as FieldAttribute;

            if (idField == null)
                Assert.Fail("The field {Id} was not able to be found and appropriately cast to a FieldAttributte type");

            Assert.AreEqual("Keyword", idField.Name);
            Assert.AreEqual(Field.Index.UN_TOKENIZED, idField.Index);
            Assert.AreEqual(Field.Store.NO, idField.Store);

            // Name Field
            PropertyInfo name = sample.GetProperty("Name");
            var nameField = name.GetCustomAttributes(typeof (FieldAttribute), false)[0] as FieldAttribute;

            if (nameField == null)
                Assert.Fail("The field {Name} was not able to be found and appropriately cast to a FieldAttributte type");

            Assert.AreEqual(null, nameField.Name);
            Assert.AreEqual(Field.Index.TOKENIZED, nameField.Index);
            Assert.AreEqual(Field.Store.YES, nameField.Store);

            // Value Field
            PropertyInfo value = sample.GetProperty("Value");
            var valueField = value.GetCustomAttributes(typeof (FieldAttribute), false)[0] as FieldAttribute;

            if (valueField == null)
                Assert.Fail("The field {Name} was not able to be found and appropriately cast to a FieldAttributte type");

            Assert.AreEqual(null, valueField.Name);
            Assert.AreEqual(Field.Index.NO, valueField.Index);
            Assert.AreEqual(Field.Store.YES, valueField.Store);
        }

        #region Nested type: SampleClass

        /// <summary>
        /// Used to provide a sample-class for test that uses the FieldAttribute type
        /// </summary>
        [Document]
        private class SampleClass
        {
            [Field(FieldIndex.UnTokenized, FieldStore.No, Name = "Keyword")]
            public string Id { get; set; }

            [Field(FieldIndex.Tokenized, FieldStore.Yes)]
            public string Name { get; set; }

            [Field(FieldIndex.No, FieldStore.Yes)]
            public string Value { get; set; }
        }

        #endregion
    }
}